package com.bien.springcloud.zuul.filter;

import com.bien.springcloud.util.RequestUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 路由之后过滤器
 */
@Component
public class AccessPostFilter extends ZuulFilter {

	private static Logger log = LoggerFactory.getLogger(AccessPostFilter.class);

	@Override
	public String filterType() {
		return "post";
	}

	@Override
	public int filterOrder() {
		return 0;
	}

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run() {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		HttpServletResponse response = ctx.getResponse();
		int status = response.getStatus();

		log.info("[###日志记录###]" + RequestUtil.getRemoteHost(request) + " 在 " + RequestUtil.getCurrentTime() + " 访问 "
				+ request.getRequestURL() + ",被访问服务的响应状态为：" + status);
		log.info("RouteHost: " + ctx.getRouteHost());
		return null;
	}

}
